#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# accept_contact_panel.py 
#
#  Copyright (c) 2006 INdT (Instituto Nokia de Technologia)
#
#  Author: Marcelo Lira dos Santos <setanta@gmail.com>
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA

import pygtk
pygtk.require("2.0")
import gtk

from gettext import gettext as _

class AcceptContactPanel(gtk.VBox):

    def __init__(self, local_contact_store):
        gtk.VBox.__init__(self, spacing=4)

        self.local_contact_store = local_contact_store

        self.pending_contacts = []
        self.current_connect_id = None

        self.message = gtk.Label()
        self.message.set_line_wrap(True)
        message = '<small>' + _("%s wants to be able to chat with you. Okay?") % "..." + '</small>'
        self.message.set_markup(message)
        self.pack_start(self.message, expand=True, fill=True)

        self.accept_button = gtk.Button()
        hbox = gtk.HBox(spacing=2)
        blabel = gtk.Label(_("Accept"))
        bicon = gtk.image_new_from_stock(gtk.STOCK_OK, gtk.ICON_SIZE_BUTTON)
        hbox.pack_start(bicon)
        hbox.pack_start(blabel)
        self.accept_button.add(hbox)
        self.accept_button.connect('clicked', \
                self.on_accept_button_clicked)

        self.reject_button = gtk.Button()
        hbox = gtk.HBox(spacing=2)
        blabel = gtk.Label(_("Reject"))
        bicon = gtk.image_new_from_stock(gtk.STOCK_CANCEL, gtk.ICON_SIZE_BUTTON)
        hbox.pack_start(bicon)
        hbox.pack_start(blabel)
        self.reject_button.add(hbox)
        self.reject_button.connect('clicked', \
                self.on_reject_button_clicked)

        self.button_hbox = gtk.HBox(spacing=4)
        self.button_hbox.pack_end(self.accept_button, expand=False, fill=False)
        self.button_hbox.pack_end(self.reject_button, expand=False, fill=False)

        #self.set_no_show_all(True)

        self.pack_start(self.button_hbox, expand=False, fill=True)


    def set_pending_contacts(self, pending_contacts):
        """
        Sets the list of persons who wants to be able to talk to you.
        """

        self.pending_contacts = pending_contacts
        self.update()

    def add_pending_contact(self, pending_contact):
        """
        Adds a person to the list of people who wants to be able to talk to you.
        """

        self.pending_contacts.append(pending_contact)
        if not self.contact_id:
            self.update()


    def update(self):
        if not self.pending_contacts:
            self.hide()
            return

        self.contact_id = self.pending_contacts.pop()
        alias = self.local_contact_store.get_alias(self.contact_id)

        if alias and alias != self.contact_id:
            person = '%s (%s)' % (alias, self.contact_id)
        else:
            person = self.contact_id

        self.message.set_markup(\
            '<small>' \
            + ( _("%s wants to be able to chat with you. Okay?") % person) \
            + '</small>')

        self.show()


    def show(self):
        """
        Show the accept/reject new contact pane.
        """
        self.set_no_show_all(False)
        self.show_all()
        self.set_no_show_all(True)


    def hide(self):
        """
        Hides the accept/reject new contact pane.
        """

        self.set_no_show_all(False)
        self.hide_all()
        self.set_no_show_all(True)


    def on_accept_button_clicked(self, widget):
        self.__authorize_contact(True)


    def on_reject_button_clicked(self, widget):
        self.__authorize_contact(False)


    def __authorize_contact(self, authorize=True):
        contact = self.local_contact_store.get_contact(self.contact_id)
        contact.authorize(authorize)

        if not authorize:
            self.local_contact_store.remove_contact(self.contact_id)

        self.contact_id = None

        self.update()

